package com.shey.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/**
 * 封装先加载图片bound，计算出inSmallSize之后再加载图片的逻辑操作
 */
public abstract class BitmapDecoder {

    private static final String TAG = "BitmapDecoder";
    /**
     * @param options
     * @return
     */
    public abstract Bitmap decodeBitmapWithOption(BitmapFactory.Options options);

    /**
     * @param width 图片的目标宽度
     * @param height 图片的目标高度
     * @return
     */
    public Bitmap decodeBitmap(int width, int height) {
        // 1、获取只加载Bitmap宽高等数据的Option, 即设置options.inJustDecodeBounds = true;
        BitmapFactory.Options options = getJustDecodeBoundsOptions();
        // 2、通过options加载bitmap，此时返回的bitmap为空,数据将存储在options中
        decodeBitmapWithOption(options);
        // 3、计算缩放比例, 并且将options.inJustDecodeBounds设置为false;
        calculateInSmall(options, width, height);
        // 4、通过options设置的缩放比例加载图片
        return decodeBitmapWithOption(options);
    }

    /**
     * @param options
     * @param width
     * @param height
     */
    protected void calculateInSmall(BitmapFactory.Options options, int width, int height) {

        // 如果请求原图,则直接加载原图,但是不能超过1M，否则压缩
        if (width <= 0 || height <= 0) {
            //设置屏幕的宽高度
            width = DisplayUtil.getScreenWidth();
            height = DisplayUtil.getScreenHeight() - DisplayUtil.getStatusBarHeight();
        }
        //设置缩放比例
        options.inSampleSize = computeInSmallSize(options, width, height);

        //图片质量
        options.inPreferredConfig = Bitmap.Config.RGB_565;

        //设置为false,解析Bitmap对象加入到内存中
        options.inJustDecodeBounds = false;
    }

    /**
     * 计算缩放比例
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private int computeInSmallSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        return sampleSize(inSampleSize, width, height, reqWidth, reqHeight);
    }

    private int sampleSize(int inSampleSize, int width, int height, int reqWidth, int reqHeight) {
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

            final float totalPixels = width * height;

            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }

        return inSampleSize;
    }

    /**
     * 获取只计算图片尺寸的参数
     * @return
     */
    private BitmapFactory.Options getJustDecodeBoundsOptions() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 设置为true,表示解析Bitmap对象，该对象不占内存
        options.inJustDecodeBounds = true;
        return options;
    }
}